#include "CEventReceiver.h"
#include "CGameManager.h"
#include "CNetworkManager.h"
#include "CState.h"

bool CEventReceiver::OnEvent(const SEvent& event)
{
  static bool cursorIsOnGui = false;

  //! GUI Event
  if (event.EventType == EET_GUI_EVENT)
  {
    s32 id = event.GUIEvent.Caller->getID();

    switch(event.GUIEvent.EventType)
    {

    case EGET_ELEMENT_LEFT:
      if ( id != -1 )
        cursorIsOnGui = false;

      if ( event.GUIEvent.Caller->getType() == EGUIET_BUTTON )
      {
        IGUIButton *btn = static_cast<IGUIButton*> (event.GUIEvent.Caller);

        //remove the border of the button.
        //btn->setDrawBorder(true);
      }


      mgr->getState()->OnGUIEvent(event);
      break;

    case EGET_ELEMENT_HOVERED:
      if ( id != -1 )
        cursorIsOnGui = true;

      //!TEST
      if ( event.GUIEvent.Caller->getType() == EGUIET_BUTTON )
      {
        IGUIButton *btn = static_cast<IGUIButton*> (event.GUIEvent.Caller);

        //remove the border of the button.
        //btn->setDrawBorder(false);
        /*
        // set the image
        btn->setImage(mgr->getDevice->getVideoDriver()->getTexture("default\\btnMC.png"));//, rect<s32>(0,0, 265, 40));
        // use the alpha channel so we get no background
        btn->setUseAlphaChannel(true);
        */
      }
      mgr->getState()->OnGUIEvent(event);
      break;

    case EGET_BUTTON_CLICKED:
      //chatbox button
      //!TODO: Global ID required
      if (id == 10 )
      {
        // network code here
        mgr->getNetworkManager()->sendChat();
        return true;
      }
      return mgr->getState()->OnButtonClick(id);
      break;

    case EGET_EDITBOX_ENTER:
      if ( id == 11 )
      {
        mgr->getNetworkManager()->sendChat();
        return true;
      }
      break;
    default:
      mgr->getState()->OnGUIEvent(event);
      break;
    }
    return false;
  }

  //! Mouse Input Event
  if(event.EventType == EET_MOUSE_INPUT_EVENT && !cursorIsOnGui) //...
  {
    switch(event.MouseInput.Event)
    {
      case EMIE_LMOUSE_PRESSED_DOWN:

        if ( !MouseIsDown[0] )
        {
            return mgr->getState()->OnMouseClick();
        }

        MouseIsDown[0] = true;
        break;

      case EMIE_LMOUSE_LEFT_UP:
        MouseIsDown[0] = false;
        break;

      default:
        break;
    }
    return false;
  }

  //! Keyboard input event
  if (event.EventType == irr::EET_KEY_INPUT_EVENT)
  {
    // There is a key change
    if ( KeyIsDown[event.KeyInput.Key] != event.KeyInput.PressedDown )
    {
      KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

      // On Key Down
      if ( KeyIsDown[event.KeyInput.Key] )
      {
        mgr->getState()->OnKeyDown(event.KeyInput.Key);
      }

      // On Key Up
      if ( !KeyIsDown[event.KeyInput.Key] )
      {
        mgr->getState()->OnKeyUp(event.KeyInput.Key);
      }

    }
  }

  return false;
}

// This is used to check whether a key is being held down
bool CEventReceiver::IsKeyDown(EKEY_CODE keyCode) const
{
  return KeyIsDown[keyCode];
}

bool CEventReceiver::IsMouseDown(s32 button)
{
  return MouseIsDown[button];
}

CEventReceiver::CEventReceiver(CGameManager* mgr)
{
  this->mgr = mgr;

  for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
    KeyIsDown[i] = false;

  for (u32 i=0; i<3; i++ )
  {
    MouseIsDown[i] = false;
  }

}
